package com.masterclient.app.modules.goods;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;

import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/8/21 17:34
 * 功能说明：
 */

public class ImageGalleryDialog extends Dialog{

    private Context context;

    private ViewPager viewPager;
    ImagesAdapter adapter;
    private RelativeLayout rlHead;

    private List<String> list;
    public ImageGalleryDialog(@NonNull Context context,List<String> list) {
        super(context,R.style.MyDialog);
        this.context = context;
        this.list = list;
    }

    public void setCurrentIndex(int position){
        if (viewPager != null) {
            viewPager.setCurrentItem(position);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_image_gallery);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        rlHead = (RelativeLayout) findViewById(R.id.rl_head);
        adapter = new ImagesAdapter();
        viewPager.setAdapter(adapter);
        rlHead.setOnClickListener(v->dismiss());
    }


    private class ImagesAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return list == null?0:list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
/*            View view = View.inflate(context,R.layout.photoview,null);
            PhotoView photoView = (PhotoView) view.findViewById(R.id.img);
            photoView.setVisibility(View.VISIBLE);
            photoView.enable();
//            photoView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            Picasso.with(context).load(list.get(position)).fit().into(photoView);
            photoView.setMaxScale(2);
            container.addView(view);

            return view;*/

            PhotoView view = new PhotoView(context);
            FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            layout.gravity = Gravity.CENTER;
            view.setLayoutParams(layout);

            view.enable();
            view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//            if(!android.text.TextUtils.isEmpty(list.get(position))){
//            App.picasso.load(list.get(position)).into(view);
//                GlideUtil.displayImage(context,list.get(position),view);
//            }
            Glide.with(context)
                    .load(list.get(position))
                    .apply(new RequestOptions().centerInside())
                    .into(view);

            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.CENTER);
            windowparams.height = ScreenUtils.getScreenHeight();
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }

    public void setCurrentPage(int position){
        viewPager.setCurrentItem(position);
    }
}
